unit uServices;

(*

  Unit that exports the Q&D service manager for the eConsole

*)

interface

Uses Classes;

Type CServiceProvider = Class( TObject )
     (* The abstract base class for all service providers *)
       Constructor Create; virtual;
       (* The constructor to create a new service provider. The remaining functions are
          left to the protocol of the service. *)
     End;
     CServiceProviderClass = Class of CServiceProvider;

Type CServiceProviderInformation = Class( TObject )
       ServiceClassName : String;
       Visual : String;
       ServiceClass : CServiceProviderClass;

       Constructor Create( aServiceClass : String; aServiceVisual : String; aClass : CServiceProviderClass );
       Function Instantiate : CServiceProvider;
     End;

Type CServiceManager = Class( TObject )
       fServices : TList;

       Constructor Create;
       Destructor Destroy; override;
       Procedure RegisterService( const aServiceClass : String; const aDescription : String; aClass : CServiceProviderClass );
       Function Services( const aServiceName : String ) : TList;
       (* Returns a list of references to CServiceProviderInformation instances. The list
          must be released by the caller. *)

       Function ProviderByVisual( const aServiceClass : String; const aServiceVisual : String ) : CServiceProviderInformation;
       (* Tries to find a service provider by its visual name - returns the first match or nil *)
     End;

Var ServiceManager : CServiceManager;

implementation

Constructor CServiceProvider.Create;
Begin
  TObject.Create;
End;

Constructor CServiceProviderInformation.Create;
Begin
  inherited Create;
  ServiceClassName := aServiceClass;
  Visual := aServiceVisual;
  ServiceClass := aClass;
End;

Function CServiceProviderInformation.Instantiate;
Begin
  Result := ServiceClass.Create;
End;

Constructor CServiceManager.Create;
Begin
  inherited Create;
  fServices := TList.Create;
End;

Destructor CServiceManager.Destroy;
Var I : Integer;
Begin
  If Assigned( fServices )
    then
      Begin
        With fServices do
          For I := 0 to Pred(Count) do
            CServiceProviderInformation( Items[I] ).Free;
        Free;
      End;
  inherited Destroy;
End;

Procedure CServiceManager.RegisterService;
Begin
  fServices.Add( CServiceProviderInformation.Create( aServiceClass, aDescription, aClass ));
End;

Function CServiceManager.Services;
Var I : Integer;
Begin
  Result := TList.Create;
  With fServices do
    For I := 0 to Pred( Count ) do
      If CServiceProviderInformation( Items[I] ).ServiceClassName = aServiceName
        then Result.Add( Items[I] );
End;

Function CServiceManager.ProviderByVisual;
Var I : Integer;
    S : TList;
Begin
  S := Services( aServiceClass );
  try
    I := 0;
    Result := nil;
    While I < S.Count do
      Begin
        If (CServiceProviderInformation( S[I] ).Visual = aServiceVisual)
          then
            Begin
              Result := S[I];
              I := S.Count;
            End;
        Inc( I );
      End;
  finally
    S.Free;
  end;
End;

initialization
  ServiceManager := CServiceManager.Create;
end.
